﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Client
{
    class TCP
    {
        private TcpClient client;
        private NetworkStream ns;
        private Thread receive;
        private Thread cn;
        private bool stopReceive;
        private string servAnswer;

        public TCP()
        {
            client = new TcpClient();
            servAnswer = "";
            stopReceive = false;
        }

        public bool Connected()
        {
            return client.Connected;
        }

        private void Connect()
        {
            client = new TcpClient();
            while (!client.Connected)
                try
                {
                    client.Connect("127.0.0.1", 13); //37.115.145.30
                    ns = client.GetStream();
                    receive.Start();
                }
                catch { }
        }

        public void Start()
        {
            receive = new Thread(Receive);
            cn = new Thread(Connect);
            stopReceive = false;
            cn.Start();
        }

        public void Disconnect()
        {
            stopReceive = true;
            if (ns != null)
                ns.Close();
            if (client != null)
                client.Close();
        }

        public void Send(string sendString)
        {
            if (ns != null && client != null)
            {
                byte[] buffer = Encoding.Default.GetBytes(sendString);
                ns.Write(buffer, 0, buffer.Length);
            }
        }

        private void Receive()
        {
            while (!stopReceive)
            {
                if (client.Connected)
                {
                    try
                    {
                        NetworkStream ns1 = client.GetStream();
                        while (ns.DataAvailable)
                        {
                            byte[] buffer = new byte[client.Available];
                            ns1.Read(buffer, 0, buffer.Length);
                            servAnswer += Encoding.Default.GetString(buffer);
                            Console.WriteLine(servAnswer);
                        }
                    }
                    catch { }
                }
                if (servAnswer != "")
                {
                    Executor();
                    servAnswer = "";
                }
                Thread.Sleep(100);
            }
        }

        private void Executor()
        {
            if (servAnswer != "")
            {
                string[] res=servAnswer.Split(' ');
                if (res[0] == "login")
                {
                    if (res[1] == "true")
                    {
                        Console.WriteLine("Server->Succesful authorization");
                        Program.authorized = true;
                    }
                    else
                        Console.WriteLine("Server->There is no such administrator or invalid login or password!!!!");
                    Program.ev.Set();
                }
                if (res[0] == "disconnection")
                {
                    Console.WriteLine("System->Disconnection from the server...");
                    Thread.Sleep(500);
                    Disconnect();
                    Program.authorized = false;
                    Console.WriteLine("System->Disconnected");
                }
                if (res[0] == "add_user")
                {
                    if (res[1] == "true")
                        Console.WriteLine("Server->User added successfully!!!");
                    else
                        Console.WriteLine("Server->Can't add user!!! Please check correctness for user data!");
                }
            }
        }

    }
}
